//
//  XJStatusViewModel.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/24.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit

class XJStatusViewModel: NSObject {

    //该viewModel为cell服务
    
    //定义一个属性
    var status: XJStatus?
    //微博认证用户图片
    var avatarImage: UIImage?
    //会员等级图片
    var memberImage:UIImage?
    //来源
    var sourceStr:NSMutableAttributedString?
    // 转发数
    var repostsCountStr: String?
    // 评论数
    var commentsCountStr: String?
    // 表态数
    var attitudesCountStr: String?
    //微博时间(制度属性,计算型)
    var sinaTimeStr: String?{
        return NSDate.sinaDate(status?.created_at ?? "")?.sinaDescripition
    }
    
    //重载构造函数
    init(status: XJStatus) {
        super.init()
        self.status = status
        //为属性赋值
        self.avatarImage = getAvatarImage(status.user?.verified)
        self.memberImage = getMemberImage(status.user?.mbrank)
        self.repostsCountStr = getSinaCount(status.reposts_count, title: "转发")
        self.commentsCountStr = getSinaCount(status.comments_count, title: "评论")
        self.attitudesCountStr = getSinaCount(status.attitudes_count, title: "赞")
        self.sourceStr = getSourceStr(status.source)
        
    }
}


extension XJStatusViewModel{
    //认证图片
    func getAvatarImage(verified: Int?) -> UIImage? {
        guard let v = verified else {
            return nil
        }
        switch v {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }

    }
    //获取会员等级图片
    func getMemberImage(mbrank: Int?) -> UIImage? {
        
        guard let mb = mbrank else {
            return nil
        }
        
        if mb > 0 && mb < 7 {
            return UIImage(named: "common_icon_membership_level\(mb)")
        }else{
            return UIImage(named: "common_icon_membership")
        }
    }
}

extension XJStatusViewModel{
    
    //获取转发评论赞的数值
    func getSinaCount(count: Int, title: String) -> String{
        
        if count <= 0{
            return title
        }else{
            if count < 10000{
                return "\(count)"
            }else{
                let sinaCount = CGFloat(count/1000)/10
                
                var sinaCountStr = "\(sinaCount)万"
                if sinaCountStr.containsString(".0"){
                    sinaCountStr = sinaCountStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return sinaCountStr
            }
        }
    }
}

//MVVM模式下  在viewModel中进行处理  ,mvc模式下在model中进行处理
extension XJStatusViewModel{
    //格式 来自XXXXXX
    func getSourceStr(source: String?) -> NSMutableAttributedString{

        guard let s = source where s.containsString("\">") else {
            let str = "君埋泉下泥销骨"
            return getSourceAttr(str)
        }
        let startRange = s.rangeOfString("\">")!
        let endRange = s.rangeOfString("</")!
        
        //截取字符串
        let result = s.substringWithRange(startRange.endIndex..<endRange.startIndex)
        
        return getSourceAttr(result)
    }
    
    //处理显示微博来源的富文本
    func getSourceAttr(result: String) -> NSMutableAttributedString{
        
        let name = "来自 " + result
        
        //创建一个可变富文本
        let attr = NSMutableAttributedString(string: name)
        //范围
        let range = (name as NSString).rangeOfString("来自")
        //设置属性
        attr.addAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], range: range)
        
        return attr
    }
    
}
















